package com.cms.recursoshumanos.facade;

import java.io.UnsupportedEncodingException;

import net.rim.device.api.ui.UiApplication;

import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.engine.ScreenEngine;
import com.cms.recursoshumanos.handler.BaseHandler;
import com.cms.recursoshumanos.handler.JParserHandler;
import com.cms.recursoshumanos.handler.WServiceCall;
import com.cms.recursoshumanos.json.object.JsonObjectGetSeguro;
import com.cms.recursoshumanos.popup.ShowDialogAlert;
import com.cms.recursoshumanos.transport.BBRequest;
import com.cms.recursoshumanos.transport.BBRequestQueue;
import com.cms.recursoshumanos.transport.HttpRequest;
import com.cms.recursoshumanos.utilities.Tool;

public class DetailPolizaScreenFacade extends BaseFacade implements BBRequest.Listener
{

	private String strUsername;
	private String strPass;
	
	private boolean flag_refresh;
	
	private HttpRequest req;
	
	ShowDialogAlert alerta;
	
	public DetailPolizaScreenFacade(String user,
										String password,
										boolean flag_refresh)
	{
		this.strUsername = user;
		this.strPass = password;
		this.flag_refresh = flag_refresh;
		
	}

	public void getDetailPoliza()
	{
		enviarSolicitud(WServiceCall.getInstance().getDetailSeguro(strUsername,strPass));
	}
	
	public void enviarSolicitud(String envelope_soap) 
	{
		try 
		{
			req = new HttpRequest();
			req.setRequestURL(Constant.URL_WEBSERVICE);	
			req.setPayload(envelope_soap, "UTF-8");
			req.setListener(this);
	        BBRequestQueue.getInstance().addRequest(req);
		} 
		catch (UnsupportedEncodingException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
  
	}
	
	public void requestStarted(BBRequest request) 
	{
		synchronized (UiApplication.getEventLock()) 
		{
			ScreenEngine.getInstance().showLoadingPopup("Cargando");
		}
		
	}
	
	public void requestCancelled(BBRequest request) 
	{
		ScreenEngine.getInstance().hideLoadingPopup();
		alerta = new ShowDialogAlert("Se cayo la conexion inesperadamente, intente nuevamente");
		alerta.run();
	}

	public void requestFailed(BBRequest request)
	{
		ScreenEngine.getInstance().hideLoadingPopup();
		alerta = new ShowDialogAlert("No se pudo conectar al servidor, intete nuevamente");
		alerta.run();
	}


	public void requestSucceeded(BBRequest request) 
	{

		String result = JParserHandler.getInstance().procesarXML(request);
		ScreenEngine.getInstance().hideLoadingPopup();
		this.setValores(result);

	}

	
	public void setValores(String strResult)
	{
			
		if (strResult.equals(null))
		{
			ShowDialogAlert alerta = new ShowDialogAlert("No se pudo obtener informacion del servidor, chequee su conexion");
			alerta.run();
		}
		else
		{
			
			JsonObjectGetSeguro.getInstance().fromJSON(strResult);
			if (JsonObjectGetSeguro.getInstance().getResult().equals(Constant.CODE_OK))
			{	
				if (!flag_refresh)
					BaseHandler.getInstance().goPoliza();
			}
			else
			{
				Tool.getInstance().showMessageError(JsonObjectGetSeguro.getInstance().getResult());
			}
			
		}
		
	}

	
}
